<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 采购基金
 */
namespace app\allwin\controller;
use app\allwin\model\Fund as FundModel;
use app\allwin\model\FundBill;
use app\allwin\model\CouponSubsidize;
use app\allwin\model\Coupon;

class Fund extends Common{

    public $mini_program   = []; 

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'采购基金','url'=>url("fund/index")]]);
    }

    /**
     * 采购基金
     * @return void
     */
    public function index(int $types = 0){
        $fund = FundModel::where($this->mini_program)->find();
        $counts = [];
        if(empty($fund)){
            $counts['balance']       = 0;
            $counts['subsidize']     = 0;
            $counts['subsidize_num'] = 0;
        }else{
            $counts['balance'] = $fund->balance;
            $counts['subsidize'] = $fund->subsidize;
            $counts['subsidize_num'] = $fund->subsidize_num;
        }
        $view['fund']  = $counts;
        if($types <= 1){
            $view['bill']  = FundBill::where($this->mini_program)->where(['types' => $types])->order('id desc')->paginate(10,false,['query'=>['types' => $types]]);
        }else{
            $view['bill']  = CouponSubsidize::with('coupon')->where($this->mini_program)->order('id desc')->paginate(10,false,['query'=>['types' => $types]]);
        }
        $view['types'] = $types;
        return view()->assign($view);
    }

    /**
     * 删除关联
     * @return void
     */
    public function delCoupon(int $id){
        $result = CouponSubsidize::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败']);
        }
    }  
    
    /**
     * 关联优惠券
     * @return void
     */
    public function winCoupon(){
        if(request()->isAjax()){
            $ids = input('post.ids/s');
            if(empty($ids)){
                return json(['code'=>0,'msg'=>'请选择要关联的优惠券']);
            }
            $ida = ids($ids,true);
            $data = [];
            foreach ($ida as $key => $value) {
                $data[$key]['member_miniapp_id'] = $this->member_miniapp_id;
                $data[$key]['coupon_id']         = $value;
                $data[$key]['how_much']          = 0;
            }
            $result = CouponSubsidize::insertAll($data);
            if($result){
                return json(['code'=>302,'msg'=>'关联补贴优惠券成功','data' =>[]]);
            }else{
                return json(['code'=>0,'msg'=>'关联补贴优惠券失败']);
            }
        }else{
            $coupon = CouponSubsidize::where($this->mini_program)->field('coupon_id')->select()->toArray();
            $coupon_ids  = [];
            if(!empty($coupon)){
                $coupon_ids = array_column($coupon,'coupon_id');
            }
            $view['lists']  = Coupon::where($this->mini_program)->whereNotIn('id',$coupon_ids)->order('size desc,id desc')->paginate(10); 
            return view()->assign($view);
        }
    } 
    
    /**
     * 排序
     */
    public function howmuch(){
        if(request()->isAjax()){
            $data = [
                'sort' => input('post.sort/f'),
                'id'   => input('post.id/d'),
            ];
            $validate = $this->validate($data,'Coupon.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = CouponSubsidize::where($this->mini_program)->where(['id' =>$data['id']])->update(['how_much' => $data['sort']]);
            if($result){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
    }
}